package com.qibo.baselib.base;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;


/**
 * Created by Administrator on 2018/1/11.
 */

public abstract class ColoredStatusBarActivity extends BaseActivity
{
    private int mColor = Color.parseColor("#FFFFFF");

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setStatusBar();


    }


    public boolean isLoading()
    {
        return false;
    }

    protected int getColor()
    {
        return mColor;
    }


    public void setStatusBar()
    {
        Window window = getWindow();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            this.getWindow()
                    .getDecorView()
                    .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }else
        {
            mColor = Color.parseColor("#999999");
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.setStatusBarColor(getColor());
            return;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            ViewGroup viewGroup = findViewById(Window.ID_ANDROID_CONTENT);

            int statusBarHeight = getStatusBarHeight(this);
            View contentView = viewGroup.getChildAt(0);
            if (contentView != null )
            {
                FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) contentView.getLayoutParams();
                ViewCompat.setFitsSystemWindows(contentView, false);
                lp.topMargin = lp.topMargin + statusBarHeight;
                Log.e("topMargin",lp.topMargin+"");
                contentView.setLayoutParams(lp);
            }

            FrameLayout decoView = (FrameLayout) getWindow().getDecorView();

            View childStatusView = decoView.getChildAt(0);

            if (childStatusView != null && childStatusView.getLayoutParams().height != statusBarHeight)
            {
                View statusView = new View(this);
                FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,statusBarHeight);
                statusView.setBackgroundColor(getColor());
                statusView.setLayoutParams(lp);
                decoView.addView(statusView,0);
            }else
            {
                childStatusView.setBackgroundColor(getColor());
            }
        }
    }

    public static int getStatusBarHeight(Context context)
    {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0)
        {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
